import java.text.SimpleDateFormat

class SystemLogController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        if(!params.sort) params.sort = "id"
        if(!params.order) params.order = "desc"
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ systemLogInstanceList: SystemLog.list( params ), systemLogInstanceTotal: SystemLog.count() ]
    }

    def listSearch = {
        // 搜索日期不为空的情况
        if (params.searchDate_value ){
            def year = params.searchDate_year
            def month = params.searchDate_month
            def day = params.searchDate_day
            def searchDateStr = year + "-" + month + "-" + day
            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd")
            Date searchDate = sdf.parse(searchDateStr)
            
            def criteria = SystemLog.createCriteria()
            def resultList = criteria.list(params){
                ge("time",searchDate)
                le("time",searchDate+1)
            }
            render(view:"listSearch" , model:[systemLogInstanceList:resultList,systemLogInstanceTotal: SystemLog.count()])
        }
        // 搜索日期为空的情况
        else
            redirect(action:'list')
    }

    def show = {
        def systemLogInstance = SystemLog.get( params.id )

        if(!systemLogInstance) {
            flash.message = "SystemLog not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ systemLogInstance : systemLogInstance ] }
    }

    def delete = {
        def systemLogInstance = SystemLog.get( params.id )
        if(systemLogInstance) {
            try {
                systemLogInstance.delete()
                flash.message = "SystemLog ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "SystemLog ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "SystemLog not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def batchdel = {
        def delrs = params.sel
        if (!delrs) {
            flash.message = "请选择日志后再删除"
            redirect(action:list)
        } else {
            def delstr = delrs.toString().contains('[') ? delrs.toString().replace('[', '(').replace(']', ')') : "(" + delrs.toString() + ")"
            try{
                SystemLog.executeUpdate("delete SystemLog a where a.id in ${delstr}")
                flash.message = "该批日志已经全部删除"
                redirect(action:list)
            }
            catch (Exception e) {
                flash.message = "不能被删"
                redirect(action:list)
            }
        }
    }

}
